# -*- coding: utf-8 -*-
# @Time   : 2023/9/18 10:30
# @Author : 大龙 🚀
# @File   : ningxia_illegal.py

from apps.tax_illegal.tax_illegal.items import NetTaxIllegalProItem
from apps.tax_illegal.tax_illegal.spiders import BaseTaxIllegalSpider
from utils.tools import urlencode, parse_url_params


class NingxiaIllegalSpider(BaseTaxIllegalSpider):
    name = "ningxia_illegal"
    province: str = "宁夏"
    url: str = "http://ningxia.chinatax.gov.cn/col/col16187/index.html"

    def start_requests(self):
        yield from self.etax_search(**{"etax_url": "https://etax.ningxia.chinatax.gov.cn:8443"})
        url = "http://ningxia.chinatax.gov.cn/module/search/index.jsp"
        for key in self.company_keys:
            params = {
                "vc_name": key,
                "field_147": "",
                "field_150": "",
                "strSelectID": "104,147,148,149,150,151",
                "i_columnid": "16187",
                "field": "field_148:1,field_149:1,vc_name:1,field_147:1,field_150:1,field_151:1",
                "initKind": "FieldForm",
                "currentplace": "",
                "splitflag": "",
                "fullpath": "0",
                "isQuery": "1",
                "download": "查询",
                "currpage": "1",
            }
            yield self.Request(url + "?" + urlencode(params), callback=self.parse_list)

    def parse_list(self, response, **kwargs):
        root_url, params = parse_url_params(response.request.url)
        page_total_num = response.xpath(".").re_first(r"共\s*(\d+)\s*页")
        yield from self.parse_detail(response, **kwargs)
        if page_total_num:
            for page in range(2, int(page_total_num) + 1):
                yield self.Request(
                    root_url + "?" + urlencode(params={**params, "currpage": f"{page}"}), callback=self.parse_detail
                )

    def parse_detail(self, response, **kwargs):
        infos = response.xpath("//table//td//table")
        for info in infos:
            item = NetTaxIllegalProItem()
            item.province = self.province
            item.company_name = info.xpath("./tr/td[1]/text()").get()
            item.year = info.xpath("./tr/td[2]/text()").get()[:10].replace("-", "")
            item.taxpayer_id = info.xpath("./tr/td[3]/text()").get()
            item.illegal_status = info.xpath("./tr/td[4]/text()").get()
            item.legal_representative = info.xpath("./tr/td[6]/text()").get()
            yield item


if __name__ == "__main__":

    from scrapy import cmdline

    cmdline.execute(argv=["scrapy", "crawl", "ningxia_illegal"])
